﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections.Specialized;
using System.Xml.Linq;

using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Configuration;
using System.Data;

public partial class Controls_ucServersList : System.Web.UI.UserControl
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
   ServerData serverData;
    public void LoadData()
    {

        foreach (var server in GetServers())
        {
            collectServerData(server);
        }
    }
   
    public void collectServerData(ServerInfo serverInfo)
    {
        TableRow tRow = new TableRow();
        table1.Rows.Add(tRow);
        serverData = new ServerData();
     
        //if(serverInfo.getDisks == "Y")
        //{
           if ((serverData.disks = WMIUtil.WMIUtilStatic.GetDiskSpace(serverInfo)) != null)
           {
            Controls_ucServerDisksSpace diskSpace = (Controls_ucServerDisksSpace)LoadControl("~/Controls/ucServerDisksSpace.ascx");
            diskSpace.serverInfo = serverInfo;
            diskSpace.disks = serverData.disks;

            TableCell tcell = new TableCell();
            tcell.Width = Unit.Percentage(60);
            tcell.VerticalAlign = VerticalAlign.Top;
            tcell.Controls.Add(diskSpace);
            tRow.Cells.Add(tcell);
            }
    //    }

     //   if (serverInfo.getServices == "Y")
     //   {
            if ((serverData.services = WMIUtil.WMIUtilStatic.GetServices(serverInfo)) != null)
            {
                Controls_ucServerServices serverservices = (Controls_ucServerServices)LoadControl("~/Controls/ucServerServices.ascx");
                serverservices.serverInfo = serverInfo;
                serverservices.serviceslist = serverData.services;

                TableCell tcell = new TableCell();
                tcell.Width = Unit.Percentage(40);
                tcell.VerticalAlign = VerticalAlign.Middle;
                tcell.Controls.Add(serverservices);
                tRow.Cells.Add(tcell);

            }
     //   }

        //if (serverInfo.getMessages == "Y")
        //{
            if((serverData.messageQ = WMIUtil.WMIUtilStatic.GetMessageQueues(serverInfo)) != null)
            {
                Controls_ucMessageQueue serverMessages = (Controls_ucMessageQueue)LoadControl("~/Controls/ucMessageQueue.ascx");
                serverMessages.serverInfo = serverInfo;
                serverMessages.Mq = serverData.messageQ;

                TableCell tcell = new TableCell();
                tcell.Width = Unit.Percentage(40);
                tcell.VerticalAlign = VerticalAlign.Middle;
                tcell.Controls.Add(serverMessages);
                tRow.Cells.Add(tcell);
            }
       // }

        //if (serverInfo.getMemory == "Y")
        //{
            if ((serverData.memory = WMIUtil.WMIUtilStatic.GetServerMemory(serverInfo)) != null)
            {
                Controls_ucServerPerformance serverMemory = (Controls_ucServerPerformance)LoadControl("~/Controls/ucServerPerformance.ascx");
                serverMemory.serverInfo = serverInfo;
                serverMemory.serverMemoryList = serverData.memory;

                TableRow ntRow = new TableRow();
                table1.Rows.Add(ntRow);
                TableCell tcell = new TableCell();
                tcell.Width = Unit.Percentage(60);
                tcell.VerticalAlign = VerticalAlign.Top;
                tcell.Controls.Add(serverMemory);
                ntRow.Cells.Add(tcell);
            }
      //  }
    }
    private List<ServerInfo> GetServers()
    {
        //List<ServerInfo> _serverInfo = new List<ServerInfo>();

        //DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["SQLExpressCon"].ConnectionString); //Server.MapPath("~/App_Data/ServerDB.mdf"));
        //Table<server> server = db.GetTable<server>();
  
            //IQueryable<server> servQuery = from serv in server select serv;
            //foreach (server serv in servQuery)
            //{
            //    if (serv.status.TrimEnd() == "Y")
            //    {
            //        _serverInfo.Add(new ServerInfo(serv.serverName, serv.userName, serv.passWord));
            //  }
            //}

        //List<ServerInfo> _serverInfo = new List<ServerInfo>();
        //XElement _x = XElement.Load(Server.MapPath("~/Data/Servers.xml"));

        //var data = from item in _x.Descendants("Server")
        //           select new
        //           {
        //               _ServerName = item.Element("Name").Value,
        //               _ServerUser = item.Element("userName").Value,
        //               _ServerPass = item.Element("passWord").Value
        //           };
        //foreach (var item in data)
        //{

        //    _serverInfo.Add(new ServerInfo(item._ServerName, item._ServerUser, item._ServerPass, "testname"));

        //}
        //return _serverInfo;

        ServerDBLinqDataContext ctx = new ServerDBLinqDataContext();

        var Query = (from servers in ctx.Servers
                     where servers.status == "Y"
                     select new ServerInfo { serverName = servers.serverName, userName = servers.userName, passWord = servers.passWord }).ToList();

        return Query; 
    }

    //[Table(Name = "dbo.Servers")]
    //public class server
    //{
    //    private int _key;
    //    [Column(IsPrimaryKey = true, Storage = "_key")]
    //    public int Key
    //    {
    //        get { return _key; }
    //        set { _key = value;} 
    //    }
    //    private string _serverName;
    //    [Column(Storage = "_serverName")]
    //    public string serverName
    //    {
    //        get { return _serverName; }
    //        set { _serverName = value; }
    //    }
    //    private string _userName;
    //    [Column(Storage = "_userName")]
    //    public string userName
    //    {
    //        get { return _userName; }
    //        set { _userName = value; }
    //    }
    //    private string _passWord;
    //    [Column(Storage = "_passWord")]
    //    public string passWord
    //    {
    //        get { return _passWord; }
    //        set { _passWord = value; }
    //    }
    //    private string _status;
    //    [Column(Storage = "_status")]
    //    public string status
    //    {
    //        get { return _status; }
    //        set { _status = value; }
    //    }
    //}
}